//
//  HMImageDownloadManager.m
//  ImageLoaderWithMKNetWorking
//
//  Created by ngoc thanh on 1/8/14.
//  Copyright (c) 2014 ngoc thanh. All rights reserved.
//

#import "HMImageDownloadManager.h"
#import "HMStoreImage.h"


@implementation HMImageDownloadManager
+ (NSOperationQueue *) lowQueue{
    static NSOperationQueue *queue = nil;
    if (queue == nil) {
        queue = [[NSOperationQueue alloc] init];
        [queue setMaxConcurrentOperationCount:1];
    }
    return queue;
}
+ (NSOperationQueue *) hightQueue{
    static NSOperationQueue *queue = nil;
    if (queue == nil) {
        queue = [[NSOperationQueue alloc] init];
        [queue setMaxConcurrentOperationCount:4];
    }
    return queue;
}
+ (HMImageDownloadManager *)share{
    static HMImageDownloadManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!sharedInstance) {
            sharedInstance = [[HMImageDownloadManager alloc] init];
        }
    });
    return sharedInstance;
}

- (MKNetworkOperation *)operDownload:(NSString *)stringUrl local:(NSString *)local complete:(MKNKResponseBlock)response errorHandler:(MKNKResponseErrorBlock)errorBlock {
    MKNetworkOperation *oper = [[MKNetworkOperation alloc] initWithURLString:stringUrl params:nil httpMethod:[self method]];
    NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:local append:NO];
    [oper addDownloadStream:stream];
    [oper addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        [[HMStoreImage share] addLocalUrl:local forOnlineUrl:stringUrl];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:stringUrl object:stringUrl];
        });
        if (response) {
            response(completedOperation);
        }
        
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        NSLog(@"%@",error.debugDescription);
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:stringUrl object:stringUrl];
        });
        if (errorBlock) {
            errorBlock(completedOperation,error);
        }
        
        
    }];
    return oper;
}

- (void)downloadUrl:(NSString *)stringUrl complete:(MKNKResponseBlock)response errorHandler:(MKNKResponseErrorBlock)errorBlock priority:(TYPELOAD)type canOverride:(BOOL)isOverride{
    NSString *local = [NSString stringWithFormat:@"%@/%@",[HMStoreImage defaultStoreImage],[[HMStoreImage share] createLocalName]];
    [self downloadUrl:stringUrl local:local complete:response errorHandler:errorBlock priority:type canOverride:isOverride];
}

- (void)downloadUrl:(NSString *)stringUrl local:(NSString *)local complete:(MKNKResponseBlock)response errorHandler:(MKNKResponseErrorBlock)errorBlock priority:(TYPELOAD)type canOverride:(BOOL)isOverride {
    if ([[HMStoreImage share] isDownloaded:stringUrl]) {
        if (isOverride) {
            [[HMStoreImage share] removeFromOnlineLink:stringUrl];
        }else{
            return;
        }
    }
    MKNetworkOperation *oper = [self operDownload:stringUrl local:local complete:response errorHandler:errorBlock];
    NSOperationQueue *queue = nil;
    switch (type) {
        case TYPELOAD_LOWPRIORITY:{
            queue = [HMImageDownloadManager lowQueue];
            break;
        }
        case TYPELOAD_HIGHTPRIORITY:{
            queue = [HMImageDownloadManager hightQueue];
            break;
        }
        default:
            break;
    }
    [queue addOperation:oper];
}

- (NSString *)method{
    return @"POST";
}
@end
